package com.core.shared;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring4.SpringTemplateEngine;

import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2016/5/10
 * Time: 15:56
 * To change this template use File | Settings | File Templates.
 */
public class MailHandle {

    private static String defaultSender = "1411802214@qq.com";

    /**
     * 普通格式邮件
     */
    public static boolean sendCommonEmail(JavaMailSender mailSender, String[] to, String subject, String text) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(defaultSender);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(text);
            mailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * html格式邮件,带内联图片
     */
    public static boolean sendHtmlEmail(JavaMailSender mailSender, String[] to, String subject, String htmlText, String servletContextPath) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
            helper.setFrom(defaultSender);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(htmlText, true);
            FileSystemResource resource = new FileSystemResource(servletContextPath + "public/img/bg2.png");
            helper.addInline("spitterLogo", resource);
            mailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * thymeleaf模版邮件
     */
    public static boolean sendThymeleafEmail(JavaMailSender mailSender, String[] to, String subject, SpringTemplateEngine thymeleaf, Map<String, Object> params) {
        try {
            Context ctx = new Context();
            ctx.setVariable("spitterName", params.get("spitterName"));
            ctx.setVariable("spitterText", params.get("spitterText"));
            String htmlText = thymeleaf.process("emailTemplate", ctx);
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
            helper.setFrom(defaultSender);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(htmlText, true);
            mailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 带附件的邮件
     */
    public static boolean sendAttachmentEmail(JavaMailSender mailSender, String[] to, String subject, String text, MultipartFile file) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
            helper.setFrom(defaultSender);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, true);
            helper.addAttachment(MimeUtility.encodeWord(file.getOriginalFilename()), file);
            mailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

}
